package com.yx.cloud.map.layer;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.MotionEvent;

import com.aaa.lib.map.MapUtils;
import com.aaa.lib.map.MapView;
import com.yx.cloud.R;
import com.iot.common.utils.LogUtils;

public class YXMoveableImageMarkerLayer extends YXImageMarkerLayer {
    private static boolean canTouch = true;
    protected float[] tmpPoint = new float[2];
    protected float[] tempMatrixValue = new float[9];
    private float lastPosX;
    private float lastPosY;
    private boolean isMove = false;

    public YXMoveableImageMarkerLayer(MapView mapView) {
        super(mapView);
        Bitmap bitmap = BitmapFactory.decodeResource(mapView.getContext().getResources(), R.mipmap.set_point);
        setMarkerBitmap(bitmap);
    }

    public static void setTouchable(boolean touchable) {
        canTouch = touchable;
    }

    @Override
    public boolean onTouch(MotionEvent event) {
        if (!canTouch || !visible) {
            return false;
        }
        LogUtils.i(" x : " + event.getX() + "y " + event.getY() + " action: " + event.getAction());
        //获取反转矩阵 ，用于获取坐标平移前的点
        MapUtils.getInverseRotatedPoint(mMapView.getTransform(), event.getX(), event.getY(), tmpPoint);
        float x = tmpPoint[0];
        float y = tmpPoint[1];


        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            lastPosX = event.getX();
            lastPosY = event.getY();
            isMove = false;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            float translateX = Math.abs(event.getX() - lastPosX);
            float translateY = Math.abs(event.getY() - lastPosY);
            if (translateX + translateY > 3) {
                //如果移动距离大于3 那么说明是在移动 不改变位置
                isMove = true;
            }
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (!isMove) {
                //没有移动 改变位置
                markerPosition.set(x, y);
                mMapView.refresh();
            }
        }
        return false;
    }

}
